﻿"""
设置ini文件
"""
import configparser
import pandas as pd

# 创建实例对象
ini_file = configparser.ConfigParser()
position_file = configparser.ConfigParser()


def del_null(x):
    if x and not pd.isna(x):
        return x
    else:
        return ''


# 读取初始化ini文件
def ini_file_read(path):
    ini_file.read(path, encoding='gbk')


# 读取ini文件，交换信息
def position_file_read(path):
    position_file.read(path, encoding='gbk')


# 保存ini文件
def ini_file_save(path):
    with open(path, 'w') as configfile:
        ini_file.write(configfile)


# 设置运动位置
def set_position():
    # 读取初始化配置文件，移除position和clamp章节
    ini_file.remove_section('Position')
    ini_file.remove_section('Clamp')

    # 读取上传配置文件的position和clamp章节并赋值给初始化配置文件
    ini_file.read_dict({'Position': dict(position_file.items('Position'))})
    ini_file.read_dict({'Clamp': dict(position_file.items('Clamp'))})


def set_position_5():
    # 读取初始化配置文件，移除position和clamp章节
    ini_file.remove_section('Position')
    # ini_file.remove_section('Clamp')
    ini_file.remove_section('Image')
    ini_file.remove_section('ImagePara1')

    # 读取上传配置文件的position和clamp章节并赋值给初始化配置文件
    ini_file.read_dict({'Position': dict(position_file.items('Position'))})
    # ini_file.read_dict({'Clamp': dict(position_file.items('Clamp'))})
    ini_file.read_dict({'Image': dict(position_file.items('Image'))})
    ini_file.read_dict({'ImagePara1': dict(position_file.items('ImagePara1'))})


# 补粉,连片设置电流电压
def set_voltage_current(voltage, current):
    # 修改配置文件的电流电压
    ini_file.set('Condtion', 'Voltage', f"{float(voltage):.2f}")
    ini_file.set('Condtion', 'Current', f"{float(current):.2f}")


# 单颗机\一体机
# 设置电流电压
def set_voltage_current_4(voltage, current):
    # 修改配置文件的电流电压
    ini_file.set('CHIP1', 'Voltage', f"{float(voltage):.2f}")
    ini_file.set('CHIP1', 'Current', f"{float(current):.2f}")


# # 补粉机设置校准系数
# def set_cal_factor(coef_data):
#     # 修改xy坐标
#     ini_file.set('Factor', '6', "1,0.0000/0.0000,0.0000/0.0000,1.0000/1.0000,"
#                                 f"{coef_data['dx']:.4f}/{coef_data['dy']:.4f},"
#                                 '1.0000/1.0000,0.0000/0.0000,1.0000/1.0000,0.0000/0.0000')
#     # 修改光通量
#     ini_file.set('Factor', '9', f"1,0.0000,0.0000,{coef_data['dlm']:.4f},0.0000,1.0000,0.0000,1.00000,0.0000")
#     # 修改电压,Ra，R9
#     factor_index = {'dU': 1, 'dRa': 10, 'dR9': 31}
#     for i in factor_index.items():
#         ini_file.set('Factor', f'{i[1]}', f'1,0.0000,0.0000,1.00000,{coef_data[i[0]]:.4f},1.0000,0.0000,1.00000,0.0000')


# 连片机设置校准系数
def set_cal_factor_3(coef_data):
    # 修改xy坐标
    ini_file.set('Factor', '4', "1,0.0000/0.0000,0.0000/0.0000,1.0000/1.0000,"
                                f"{coef_data['dx']:.4f}/{coef_data['dy']:.4f},"
                                '1.0000/1.0000,0.0000/0.0000,1.0000/1.0000,0.0000/0.0000')
    # 修改光通量
    ini_file.set('Factor', '2', f"1,0.0000,0.0000,{coef_data['dlm']:.4f},0.0000,1.0000,0.0000,1.00000,0.0000")
    # 修改电压,Ra，R9
    factor_index = {'dU': 1, 'dRa': 6, 'dR9': 7}
    for i in factor_index.items():
        ini_file.set('Factor', f'{i[1]}', f'1,0.0000,0.0000,1.00000,{coef_data[i[0]]:.4f},1.0000,0.0000,1.00000,0.0000')


# 单颗机设置校准系数
def set_cal_factor_4(coef_data, range_data, bin_range):
    # 修改xy坐标
    ini_file.set('CHIP1', 'Para6', f"1,0,{1 if 'CIExy' in list(bin_range['Parameter']) else 0},"
                                   f"0.0000/0.0000,0.0000/0.0000,1.0000/1.0000,{coef_data['dx']:.4f}/{coef_data['dy']:.4f},0,0,0")

    # 修改电压,Ra，R9,光通量
    factor_index = {'Ra': 'Para10', 'R9': 'Para31', 'LuminousFlux_lm': 'Para7',
                    'SDCM': 'Para19'}
    other_index = {'Ra': 'dRa', 'R9': 'dR9', 'LuminousFlux_lm': 'dlm'}
    for i in range_data.itertuples():
        if getattr(i, 'Parameter') == 'LuminousFlux_lm':
            ini_file.set('CHIP1', 'Para7',
                         f"1,1,{1 if 'LuminousFlux_lm' in list(bin_range['Parameter']) else 0}"
                         f",0.0000,0.0000,{coef_data['dlm']:.4f},0.00000,"
                         f"{getattr(i,'LSL')},"
                         f"{getattr(i,'USL')},"
                         f"{getattr(i,'FailedBinID')}")
        elif getattr(i, 'Parameter') == 'SDCM':
            ini_file.set('CHIP1', 'Para19',
                         f"0,1,"
                         f"{1 if 'SDCM' in list(bin_range['Parameter']) else 0}"
                         f",0,0,1,0,{getattr(i,'LSL')},"
                         f"{getattr(i,'USL')},"
                         f"{getattr(i,'FailedBinID')}")
        # 2020/12/28：添加分光校准，电压系数选项勾选
        elif getattr(i,'Parameter') == 'ForwardVoltage_V':
            ini_file.set('CHIP1', 'Para1',
                         f"1,1,{1 if 'ForwardVoltage_V' in list(bin_range['Parameter']) else 0}"
                         f",0.0000,0.0000,1.0000,{coef_data['dU']:.4f},"
                         f"{getattr(i, 'LSL')},"
                         f"{getattr(i, 'USL')},"
                         f"{getattr(i, 'FailedBinID')}")
        else:
            if getattr(i, 'Parameter') in factor_index.keys():
                ini_file.set('CHIP1', f"{factor_index.get(getattr(i,'Parameter'))}",
                             f"1,1,0,0.0000,0.0000,1.00000,"
                             f"{coef_data[other_index[getattr(i,'Parameter')]]:.4f},{getattr(i,'LSL')},"
                             f"{getattr(i,'USL')},"
                             f"{getattr(i,'FailedBinID')}")
    # 防止没有 R9 的情况
    if 'R9' not in list(range_data['Parameter']):
        ini_file.set('CHIP1', f"{factor_index['R9']}",
                     f"1,0,0,0.0000,0.0000,1.00000,"
                     f"{coef_data['dR9']:.4f},0,0,0")
    # 防止没有 Ra 的情况
    if 'Ra' not in list(range_data['Parameter']):
        ini_file.set('CHIP1', f"{factor_index['Ra']}",
                     f"1,0,0,0.0000,0.0000,1.00000,"
                     f"{coef_data['dRa']:.4f},0,0,0")
    # 防止没有 lm 的情况
    if 'LuminousFlux_lm' not in list(range_data['Parameter']):
        ini_file.set('CHIP1', f"{factor_index['LuminousFlux_lm']}",
                     f"1,0,0,0.0000,0.0000,{coef_data['dlm']:.4f},"
                     f"0.000,0,0,0")


# 一体机
def set_cal_factor_5(coef_data, range_data, bin_range):
    ini_file.set('Condtion', 'PolSet', '1')
    # 修改xy坐标
    ini_file.set('CHIP1', 'Para6',
                 f"1,0,{1 if 'CIExy' in list(bin_range['Parameter']) else 0},"
                 f"0.0000/0.0000,0.0000/0.0000,1.0000/1.0000,{coef_data['dx']:.4f}/{coef_data['dy']:.4f},0,0,0")
    # 修改电压,Ra，R9,光通量
    factor_index = {'Ra': 'Para10', 'R9': 'Para31', 'LuminousFlux_lm': 'Para7',
                    'SDCM': 'Para19', 'ZenerVoltage_V': 'Para40'}
    other_index = {'Ra': 'dRa', 'R9': 'dR9', 'LuminousFlux_lm': 'dlm'}
    for i in range_data.itertuples():
        if getattr(i, 'Parameter') == 'LuminousFlux_lm':
            ini_file.set('CHIP1', 'Para7',
                         f"1,1,{1 if 'LuminousFlux_lm' in list(bin_range['Parameter']) else 0}"
                         f",0.0000,0.0000,{coef_data['dlm']:.4f},0.00000,"
                         f"{getattr(i,'LSL')},"
                         f"{getattr(i,'USL')},"
                         f"{getattr(i,'FailedBinID')}")
        elif getattr(i, 'Parameter') == 'SDCM':
            ini_file.set('CHIP1', 'Para19',
                         f"0,1,"
                         f"{1 if 'SDCM' in list(bin_range['Parameter']) else 0}"
                         f",0,0,1,0,{getattr(i, 'LSL')},"
                         f"{getattr(i, 'USL')},"
                         f"{getattr(i, 'FailedBinID')}")
        elif getattr(i, 'Parameter') == 'ForwardVoltage_V':
            ini_file.set('CHIP1', 'Para1',
                         f"1,1,"
                         f"{1 if 'ForwardVoltage_V' in list(bin_range['Parameter']) else 0}"
                         f",0,0,1,{coef_data['dU']},{getattr(i, 'LSL')},"
                         f"{getattr(i, 'USL')},"
                         f"{getattr(i, 'FailedBinID')}")
        # 齐纳电压
        elif getattr(i, 'Parameter') == 'ZenerVoltage_V':
            ini_file.set('CHIP1', 'Para40',
                         f"0,0,{1 if 'ZenerVoltage_V' in list(bin_range['Parameter']) else 0},"
                         f"0,0,1,0,{getattr(i,'LSL')},"
                         f"{getattr(i,'USL')},"
                         f"{getattr(i,'FailedBinID')}")
            ini_file.set('Condtion', 'PolSet', '0')
        else:
            if getattr(i, 'Parameter') in factor_index.keys():
                ini_file.set('CHIP1', f"{factor_index.get(getattr(i,'Parameter'))}",
                             f"1,1,0,0.0000,0.0000,1.00000,"
                             f"{coef_data[other_index[getattr(i,'Parameter')]]:.4f},{getattr(i,'LSL')},"
                             f"{getattr(i,'USL')},"
                             f"{getattr(i,'FailedBinID')}")
    # 防止没有 R9 的情况
    if 'R9' not in list(range_data['Parameter']):
        ini_file.set('CHIP1', f"{factor_index['R9']}",
                     f"1,0,0,0.0000,0.0000,1.00000,"
                     f"{coef_data['dR9']:.4f},0,0,0")
    # 防止没有 Ra 的情况
    if 'Ra' not in list(range_data['Parameter']):
            ini_file.set('CHIP1', f"{factor_index['Ra']}",
                         f"1,0,0,0.0000,0.0000,1.00000,"
                         f"{coef_data['dRa']:.4f},0,0,0")
    # 防止没有 lm 的情况
    if 'LuminousFlux_lm' not in list(range_data['Parameter']):
        ini_file.set('CHIP1', f"{factor_index['LuminousFlux_lm']}",
                     f"1,0,0,0.0000,0.0000,{coef_data['dlm']:.4f},"
                     f"0.000,0,0,0")


# # 补粉设置成品参数范围
# def set_range(range_data):
#     range_index = {'LuminousFlux_lm': 9, 'ForwardVoltage_V': 1, 'SDCM': 19, 'Ra': 10, 'R9': 31}
#     for i in range_index.items():
#         if i[0] in list(range_data['Parameter']):
#             # 以SDCM分级的范围 固定为0~2
#             if i[0] == 'SDCM':
#                 ini_file.set('Range',f'{i[1]}','0,2,10')
#             else:
#                 # 判断有范围值再设置
#                 if range_data[range_data['Parameter'] == i[0]]['USL'].to_list()[0]:
#                     ini_file.set('Range', f'{i[1]}', f"1,{range_data[range_data['Parameter'] == i[0]]['LSL'].to_list()[0]},"
#                                                      f"{range_data[range_data['Parameter'] == i[0]]['USL'].to_list()[0]},10")


# 连片设置成品参数范围
def set_range_3(range_data):
    range_index = {'LuminousFlux_lm': 2, 'ForwardVoltage_V': 1, 'SDCM': 8, 'Ra': 6, 'R9': 7}
    for i in range_index.items():
        if i[0] in list(range_data['Parameter']):
            ini_file.set('Range', f'{i[1]}', f"1,{range_data[range_data['Parameter'] == i[0]]['LSL'].to_list()[0]},"
                                             f"{range_data[range_data['Parameter'] == i[0]]['USL'].to_list()[0]},"
                                             f"{range_data[range_data['Parameter'] == i[0]]['FailedBinID'].to_list()[0]}")

# 连片设置色容差bin
def set_mac_bin_3(range_data, bin_range, extra_bin):
    # 设置级外Bin
    extra_list = {'BinOut': '级外bin', 'BinNG': '死灯bin', 'BinSata': '饱和bin'}
    for y in extra_list.items():
        if y[1] in list(extra_bin['OutBinName']):
            ini_file.set('NGBin', f'{y[0]}', f"{extra_bin[extra_bin['OutBinName'] == y[1]]['BinID'].to_list()[0]}")
    lm_dict = {}  # 光通量字典
    SDCM_dict = {}  # SDCM字典
    xy_dict = {}  # xy值的字典
    U_dict = {}     # 电压字典
    name_dict = {}  # BinName对应字典
    id_list = []    # 存储binID
    if 'ForwardVoltage_V' in list(bin_range['Parameter']):
        ini_file.set('BinSel', '1', '1')
    if 'LuminousFlux_lm' in list(bin_range['Parameter']):
        ini_file.set('BinSel', '2', '1')
    if 'CIExy' in list(bin_range['Parameter']):
        ini_file.set('BinSel', '4', '1')
    if 'SDCM' in list(bin_range['Parameter']):
        ini_file.set('BinSel', '8', '1')
    # 设置分级数据
    for i in bin_range.itertuples():
        if getattr(i,'Parameter') == 'LuminousFlux_lm':
            id_list.append(getattr(i,'BinID'))
            name_dict[getattr(i,'BinID')] = getattr(i,'BinName')
            lm_dict[getattr(i,'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')},"
        elif getattr(i, 'Parameter') == 'CIExy':
            id_list.append(getattr(i, 'BinID'))
            name_dict[getattr(i, 'BinID')] = getattr(i, 'BinName')
            xy_dict[
                getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')},{del_null(getattr(i, 'CIEx2'))}," \
                f"{del_null(getattr(i, 'CIEy2'))},{del_null(getattr(i, 'CIEx3'))},{del_null(getattr(i, 'CIEy3'))}," \
                f"{del_null(getattr(i, 'CIEx4'))},{del_null(getattr(i, 'CIEy4'))},"
        elif getattr(i,'Parameter') == 'SDCM':
            id_list.append(getattr(i, 'BinID'))
            name_dict[getattr(i, 'BinID')] = getattr(i, 'BinName')
            SDCM_dict[getattr(i,'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')}"
        elif getattr(i, 'Paramter') == 'ForwardVoltage_V':
            id_list.append(getattr(i, 'BinID'))
            name_dict[getattr(i, 'BinID')] = getattr(i, 'BinName')
            U_dict[getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')}"
    id_list = sorted(list(set(id_list)))
    for id in id_list:
        ini_file.set('BinData', f"{id}",
                     f"{id},{name_dict[id]},-8323200,"
                     f"{del_null(U_dict.get(id))+del_null(lm_dict.get(id))+del_null(xy_dict.get(id))+del_null(SDCM_dict.get(id))}")
    # 设置不良Bin
    NGBin_dic = {'SDCM': '色温不良', 'LuminousFlux_lm': '光通量不良', 'Ra': 'Ra不良', 'R9': 'R9不良', 'ForwardVoltage_V': '电压不良'}
    for x in range_data.itertuples():
        # 存在该不良bin,才进行更改
        if getattr(x, 'Parameter') in NGBin_dic.keys():
            if getattr(x, 'Parameter') == 'SDCM':
                ini_file.set('NGBin', 'BinOut', f"{getattr(x, 'FailedBinID')}")
            ini_file.set('BinData', f"{getattr(x, 'FailedBinID')}",
                         f"{getattr(x, 'FailedBinID')},{NGBin_dic[getattr(x, 'Parameter')]},-8323200,,")


# 单颗机设置色容差NGBin,BinData
def set_mac_bin_4(range_data, bin_range, extra_bin):
    # 设置级外Bin
    if 'NGBin' not in ini_file.sections():
        ini_file.add_section('NGBin')
    if 'BinData' not in ini_file.sections():
        ini_file.add_section('BinData')
    extra_list = {'BinAOI': 'AOI不良bin', 'BinNG': '死灯bin', 'BinOut': '级外bin', 'BinSata': '饱和bin', 'BinMark': '扫描不良'}
    for y in extra_list.items():
        if y[1] in list(extra_bin['OutBinName']):
            ini_file.set('NGBin', f'{y[0]}', f"{extra_bin[extra_bin['OutBinName'] == y[1]]['BinID'].to_list()[0]}")

    # 设置第0行的
    ini_file.set('BinData', '0', '0,NG*NG,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,')
    # 判断Parameter中有无CIExy
    if len(bin_range):
        # 2020/12/28：添加电压的分级数据获取
        voltage_dict = {}  # 电压字典
        xy_dict = {}  # xy值的字典
        SDCM_dict = {}  # SDCM字典
        lm_dict = {}  # 光通量字典
        bin_list = []  # 对应BinID列表
        bin_name = {}  # BinID-BinName
        for i in bin_range.itertuples():
            if getattr(i, 'Parameter') == 'ForwardVoltage_V':
                voltage_dict[getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')},"
                bin_list.append(getattr(i, 'BinID'))
                if not bin_name.get(getattr(i, 'BinID')):
                    bin_name[getattr(i, 'BinID')] = f"{getattr(i, 'BinID')},{getattr(i, 'BinName')},"
            elif getattr(i, 'Parameter') == 'CIExy':
                xy_dict[
                    getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')},{del_null(getattr(i, 'CIEx2'))}," \
                                           f"{del_null(getattr(i, 'CIEy2'))},{del_null(getattr(i, 'CIEx3'))},{del_null(getattr(i, 'CIEy3'))}," \
                                           f"{del_null(getattr(i, 'CIEx4'))},{del_null(getattr(i, 'CIEy4'))},"
                bin_list.append(getattr(i, 'BinID'))
                if not bin_name.get(getattr(i, 'BinID')):
                    bin_name[getattr(i, 'BinID')] = f"{getattr(i, 'BinID')},{getattr(i, 'BinName')},"
            elif getattr(i, 'Parameter') == 'LuminousFlux_lm':
                lm_dict[getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i,'USL')},"
                bin_list.append(getattr(i, 'BinID'))
                if not bin_name.get(getattr(i, 'BinID')):
                    bin_name[getattr(i, 'BinID')] = f"{getattr(i, 'BinID')},{getattr(i, 'BinName')},"
            elif getattr(i, 'Parameter') == 'SDCM':
                # 添加对SDCM的判断
                if getattr(i, 'USL'):
                    SDCM_dict[getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')},0.00,360.00,"
                else:
                    SDCM_dict[getattr(i, 'BinID')] = f"0.00,360.00,"
                bin_list.append(getattr(i, 'BinID'))
                if not bin_name.get(getattr(i, 'BinID')):
                    bin_name[getattr(i, 'BinID')] = f"{getattr(i, 'BinID')},{getattr(i, 'BinName')},"
        if bin_list:
            for i in bin_list:
                ini_file.set('BinData', f'{i}',
                             f"{del_null(bin_name.get(i))+del_null(voltage_dict.get(i))+del_null(xy_dict.get(i))+del_null(lm_dict.get(i))+del_null(SDCM_dict.get(i))}")
        # print(xy_dict,lm_dict,SDCM_dict)
    # 设置不良Bin
    NGBin_dic = {'SDCM': '色温不良', 'LuminousFlux_lm': '光通量不良', 'Ra': 'Ra不良', 'R9': 'R9不良', 'ForwardVoltage_V': '电压不良'}
    for x in range_data.itertuples():
        if getattr(x, 'Parameter') in NGBin_dic.keys():
            if getattr(x, 'Parameter') == 'SDCM':
                ini_file.set('NGBin', 'BinOut', f"{getattr(x, 'FailedBinID')}")
            ini_file.set('BinData', f"{getattr(x, 'FailedBinID')}",
                         f"{getattr(x, 'FailedBinID')},{NGBin_dic[getattr(x, 'Parameter')]},,,,,,,")


# 一体机设置色容差NGBin,BinData
def set_mac_bin_5(range_data, bin_range, extra_bin):
    # 设置级外Bin
    if 'NGBin' not in ini_file.sections():
        ini_file.add_section('NGBin')
    if 'BinData' not in ini_file.sections():
        ini_file.add_section('BinData')
    extra_list = {'BinShort': '短路bin', 'BinAOI': 'AOI不良bin', 'BinOut': '级外bin', 'BinNG': '死灯bin', 'BinSata': '饱和bin',
                  'BinMark': '扫描不良'}
    for y in extra_list.items():
        if y[1] in list(extra_bin['OutBinName']):
            ini_file.set('NGBin', f'{y[0]}', f"{extra_bin[extra_bin['OutBinName'] == y[1]]['BinID'].to_list()[0]}")

    # 设置第0行的
    ini_file.set('BinData', '0', '0,点不亮,,,,')
    # 判断Parameter中有无CIExy
    if len(bin_range):
        # 2020/12/28:添加电压数据的分级数据
        voltage_dict = {}  # 电压字典
        xy_dict = {}  # xy值得字典
        SDCM_dict = {}  # SDCM字典
        lm_dict = {}  # 光通量字典
        bin_list = []  # 对应BinID列表
        bin_name = {}  # BinID-BinName
        for i in bin_range.itertuples():
            if getattr(i, 'Parameter') == 'ForwardVoltage_V':
                voltage_dict[getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')},"
                bin_list.append(getattr(i, 'BinID'))
                if not bin_name.get(getattr(i, 'BinID')):
                    bin_name[getattr(i, 'BinID')] = f"{getattr(i, 'BinID')},{getattr(i, 'BinName')},"
            elif getattr(i, 'Parameter') == 'CIExy':
                xy_dict[
                    getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i, 'USL')},{del_null(getattr(i, 'CIEx2'))}," \
                                           f"{del_null(getattr(i, 'CIEy2'))},{del_null(getattr(i, 'CIEx3'))},{del_null(getattr(i, 'CIEy3'))}," \
                                           f"{del_null(getattr(i, 'CIEx4'))},{del_null(getattr(i, 'CIEy4'))},"
                bin_list.append(getattr(i, 'BinID'))
                if not bin_name.get(getattr(i, 'BinID')):
                    bin_name[getattr(i, 'BinID')] = f"{getattr(i, 'BinID')},{getattr(i, 'BinName')},"
            elif getattr(i, 'Parameter') == 'LuminousFlux_lm':
                lm_dict[getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i,'USL')},"
                bin_list.append(getattr(i, 'BinID'))
                if not bin_name.get(getattr(i, 'BinID')):
                    bin_name[getattr(i, 'BinID')] = f"{getattr(i, 'BinID')},{getattr(i, 'BinName')},"
            elif getattr(i, 'Parameter') == 'SDCM':
                # 添加对SDCM的判断
                if getattr(i,'USL'):
                    SDCM_dict[getattr(i, 'BinID')] = f"{getattr(i, 'LSL')},{getattr(i,'USL')},0.00,360.00,"
                else:
                    SDCM_dict[getattr(i, 'BinID')] = f"0.00,360.00,"
                bin_list.append(getattr(i, 'BinID'))
                if not bin_name.get(getattr(i, 'BinID')):
                    bin_name[getattr(i, 'BinID')] = f"{getattr(i, 'BinID')},{getattr(i, 'BinName')},"
        if bin_list:
            for i in bin_list:
                ini_file.set('BinData', f'{i}',
                             f"{del_null(bin_name.get(i))+del_null(voltage_dict.get(i))+del_null(xy_dict.get(i))+del_null(lm_dict.get(i))+del_null(SDCM_dict.get(i))}")
        # print(xy_dict, lm_dict, SDCM_dict)
    # 设置不良Bin
    NGBin_dic = {'SDCM': '色温不良', 'LuminousFlux_lm': '光通不良', 'Ra': '显指不良', 'R9': 'R9不良',
                 'ForwardVoltage_V': '电压不良', 'ZenerVoltage_V': '齐纳电压不良'}
    for x in range_data.itertuples():
        if getattr(x, 'Parameter') in NGBin_dic.keys():
            if getattr(x, 'Parameter') == 'SDCM':
                # ini_file.set('BinData', 'XYPoints', f"{getattr(x, 'FailedBinID')}")
                ini_file.set('NGBin', 'BinOut', f"{getattr(x, 'FailedBinID')}")
            ini_file.set('BinData', f"{getattr(x, 'FailedBinID')}",
                         f"{getattr(x, 'FailedBinID')},{NGBin_dic[getattr(x, 'Parameter')]},,,,")


# 设置成品颜色标准
def set_color_stand(color_data):
    if 'Data' not in ini_file.sections():
        ini_file.add_section('Data')
    ini_file.set('Data', '1',
                 f"{color_data.loc[0, 'CCT_code_alias']}({color_data.loc[0, 'CCT_STANDARD']}),1,"
                 f"{color_data.loc[0, 'a']},{color_data.loc[0, 'b']},{color_data.loc[0, 'θ']},"
                 f"{color_data.loc[0, 'CIEx']},{color_data.loc[0, 'CIEy']}")


# 设置成品颜色标准
def set_color_stand_4(color_data):
    if 'Data' not in ini_file.sections():
        ini_file.add_section('Data')
    ini_file.set('Data', '1',
                 f"{color_data.loc[0, 'CCT_code_alias']}({color_data.loc[0, 'CCT_STANDARD']}),"
                 f"{color_data.loc[0, 'a']},{color_data.loc[0, 'b']},{color_data.loc[0, 'θ']},"
                 f"{color_data.loc[0, 'CIEx']},{color_data.loc[0, 'CIEy']}")


# 一体机选择颜色标准
def set_color_stand_5(color_data):
    id = color_data.loc[0, 'ID']
    ini_file.set('CHIP1', 'SDCMStd', f"{id}")


# 设置湿胶参数范围
def set_wet_range(range_data):
    range_index = {'Wet_Glue_lm': 9, 'Wet_Glue_Ra': 10, 'Wet_Glue_R9': 31}
    for i in range_index.items():
        if range_data.loc[0,i[0]]:
            ini_file.set('Range', f'{i[1]}', f"1,{range_data.loc[0, i[0]]},{abs(range_data.loc[0, i[0]] * 10)},10")
        else:
            ini_file.set('Range', f'{i[1]}', f"0,,,10")


# 设置小批量与量产湿胶颜色标准
def set_wet_xy(color_data, wet_xy, pes):
    # 1为小批量
    if pes == '1':
        ini_file.set('Data', '1',
                     f"{color_data.loc[0, 'CCT_code_alias']}({color_data.loc[0, 'CCT_STANDARD']}小批量),1,"
                     f"{color_data.loc[0, 'a']},{color_data.loc[0, 'b']},{color_data.loc[0, 'θ']},"
                     f"{wet_xy.loc[0, 'initial_CIEx']},{wet_xy.loc[0, 'initial_CIEy']}")
    # 2位量产湿胶
    elif pes == '2':
        ini_file.set('Data', '1',
                     f"{color_data.loc[0, 'CCT_code_alias']}({color_data.loc[0, 'CCT_STANDARD']}湿胶),1,"
                     f"{color_data.loc[0, 'a']},{color_data.loc[0, 'b']},{color_data.loc[0, 'θ']},"
                     f"{wet_xy.loc[0, 'Adjust_CIEx']},{wet_xy.loc[0, 'Adjust_CIEy']}")

    else:
        pass

# if __name__ == '__main__':
# ini_file_bac_read(r'ini\init00.ini')
# ini_file_read(r'ini\init00.ini')
# set_voltage_current(111, 222)
# ini_file_save(r'ini\配置.ini')
# print(ini_file.items('Condtion'))
